package org.jeecg.modules.content.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.MD5Util;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.content.entity.ConContentTemplate;
import org.jeecg.modules.content.entity.ConContentVersion;
import org.jeecg.modules.content.entity.ContentGoto;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.model.ConContentTemplateDetailModel;
import org.jeecg.modules.content.model.ConContentTemplateModel;
import org.jeecg.modules.content.service.IConContentTemplateService;
import org.jeecg.modules.content.service.IConContentVersionService;
import org.jeecg.modules.content.service.IContentGotoService;
import org.jeecg.modules.innertopic.entity.InnerTopic;
import org.jeecg.third.fangzheng.FangApiUtil;
import org.jeecg.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @Description: 内容模版
* @Author: wufanxin
* @Date:   2020-07-06
* @Version: V1.0
*/
@Slf4j
@Api(tags="内容模版信息")
@RestController
@RequestMapping("/content/contentTemplate")
public class ContentTemplateController extends JeecgController<ConContentTemplate, IConContentTemplateService> {

   @Autowired
   private IConContentTemplateService templateService;
   @Autowired
   private FangApiUtil fangApiUtil;

   @Autowired
   private IContentGotoService contentGotoService;

    /**
     * 内容模版列表
     * @param req
     * @return
     */
    @ApiOperation(value = "内容模版信息-内容模版列表", notes = "内容模版信息-内容模版列表")
    @GetMapping(value = "/templateList")
    public Result<?> templateList(HttpServletRequest req, ConContentTemplate template,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<ConContentTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag","0");

        if(!StringUtils.isEmpty(template.getContentTitle())){
            queryWrapper.like("content_title",template.getContentTitle());
        }
        queryWrapper.orderByDesc("create_time");
        Page<ConContentTemplate> page = new Page<ConContentTemplate>(pageNo, pageSize);
        IPage<ConContentTemplate> pageList = templateService.page(page, queryWrapper);
        return Result.ok(pageList);
    }




    /**
     * 内容模版列表
     * @return
     */
    @GetMapping(value = "/getBanners")
    public Result<?> getBanners() {
        JSONArray banners = fangApiUtil.getBanners();

        return Result.ok(banners);
    }

    /**
     * 同步
\     * @return
     */
    @PostMapping(value = "/syncAdd")
    public Result<?> getSyncAdd(ConContentTemplate conContentTemplate,String columId,String columName) {
        String  result = fangApiUtil.syncAdd(conContentTemplate, columId, columName);


        if(result !=null ){
            JSONObject jsonObject =JSONObject.parseObject(result);
            if(jsonObject.get("success").toString().equals("true")){
                conContentTemplate.setSyncStatus(1);
                templateService.updateById(conContentTemplate);
            }else {
                conContentTemplate.setSyncStatus(3);
                templateService.updateById(conContentTemplate);
                return Result.error("同步失败！");
            }
        }else {
            return Result.error("同步失败！");
        }


        return Result.ok();
    }


    @GetMapping(value = "/callBackStatus")
    public Result<?> callBackStatus(String docId,String token) {
        String s = MD5Util.MD5Encode(docId + "YL","utf-8");
        if(s.equals(token)){
            ConContentTemplate conContentTemplate = new ConContentTemplate();
            conContentTemplate.setId(docId);
            conContentTemplate.setSyncStatus(3);
            templateService.updateById(conContentTemplate);
            return Result.ok();
       }else {
            return Result.error("token验证失败");
        }

    }


    /**
     * 添加模版
     * @param req
     * @return
     */
    @ApiOperation(value = "内容模版信息-添加模版", notes = "内容模版信息-添加模版")
    @PostMapping(value = "/addTemplate")
    public Result<?> addTemplate(HttpServletRequest req,@RequestBody ConContentTemplate template) {
        template.setDelFlag(0);
        template.setId(UUIDGenerator.generate());
        template.setPcLogoUrl(template.getMbLogoUrl());
        templateService.save(template);

        ContentGoto contentGoto = new ContentGoto();
        contentGoto.setId(UUIDGenerator.generate());
        contentGoto.setContent_id(template.getContentNo());
        contentGoto.setDest(1);
        contentGotoService.save(contentGoto);

        return Result.ok();
    }


    /**
     * 删除模版
     * @param req
     * @return
     */
    @ApiOperation(value = "内容模版信息-删除模版", notes = "内容模版信息-删除模版")
    @GetMapping(value = "/deleteTemplate")
    public Result<?> deleteTemplate(HttpServletRequest req, @RequestParam(name="id")String id) {
        UpdateWrapper<ConContentTemplate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("del_flag",1);
        updateWrapper.eq("id",id);
        templateService.update(updateWrapper);
        return Result.ok();
    }


    /**
     *
     * @param conContentTemplateModel
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "绩效管理-文章质量与转载管理")
    @ApiOperation(value="绩效管理-分页列表查询", notes="绩效管理-文章质量与转载管理")
    @GetMapping(value = "/perforList")
    public Result<?> queryPerforPageList(ConContentTemplateModel conContentTemplateModel,
                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
    ) {

        Page<ConContentTemplateModel> page = new Page<ConContentTemplateModel>(pageNo, pageSize);
        Page<ConContentTemplateModel> result = templateService.getPerList(page, conContentTemplateModel);

        return Result.ok(result);
    }

    /**
     *
     * @param templateId
     * @return
     */
    @AutoLog(value = "绩效管理-文章质量与转载管理详情")
    @ApiOperation(value="绩效管理-分页列表查询", notes="绩效管理-文章质量与转载管理详情\"")
    @GetMapping(value = "/perforDetail")
    public Result<?> getDetail(String templateId)
    {
        ConContentTemplateDetailModel detail = templateService.getDetail(templateId);

        return Result.ok(detail);
    }

}
